<?php
// 简单的 Worker 测试
$requestCount = 0;
$startTime = time();

$handler = static function () use (&$requestCount, $startTime) {
    $requestCount++;
    $currentTime = date('Y-m-d H:i:s');
    $runningTime = time() - $startTime;
    echo "Request #$requestCount".PHP_EOL;
    echo "Current time: $currentTime\n";
    echo "Worker running for: {$runningTime}s\n";
    echo "Process ID: " . getmypid() . "\n";
    echo "Memory: " . round(memory_get_usage() / 1024 / 1024, 2) . " MB\n";
    echo "==============================\n\n";
    return true;
};

// 主循环
echo "Worker started at " . date('Y-m-d H:i:s') . "\n";
echo "Process ID: " . getmypid() . "\n\n";

while (true) {
    $continue = frankenphp_handle_request($handler);
    if (!$continue) {
        break;
    }

    // 可选：每处理 10 个请求后输出状态
    if ($requestCount % 2 === 0) {
        error_log("Processed $requestCount requests so far...");
    }
}